{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn import datasets  \n",
    "from sklearn import model_selection  \n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.neighbors import KNeighborsClassifier  \n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.ensemble import VotingClassifier\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.91 [KNN]\n",
      "Accuracy: 0.93 [Decision Tree]\n",
      "Accuracy: 0.91 [LogisticRegression]\n",
      "Accuracy: 0.93 [VotingClassifier]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "F:\\Anaconda3\\lib\\site-packages\\sklearn\\preprocessing\\label.py:171: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.\n",
      "  if diff:\n",
      "F:\\Anaconda3\\lib\\site-packages\\sklearn\\preprocessing\\label.py:171: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.\n",
      "  if diff:\n",
      "F:\\Anaconda3\\lib\\site-packages\\sklearn\\preprocessing\\label.py:171: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.\n",
      "  if diff:\n"
     ]
    }
   ],
   "source": [
    "# 载入数据集\n",
    "iris = datasets.load_iris()  \n",
    "# 只要第1,2列的特征\n",
    "x_data, y_data = iris.data[:, 1:3], iris.target  \n",
    "\n",
    "# 定义三个不同的分类器\n",
    "clf1 = KNeighborsClassifier(n_neighbors=1)  \n",
    "clf2 = DecisionTreeClassifier() \n",
    "clf3 = LogisticRegression()  \n",
    "\n",
    "sclf = VotingClassifier([('knn',clf1),('dtree',clf2), ('lr',clf3)])   \n",
    "  \n",
    "for clf, label in zip([clf1, clf2, clf3, sclf],\n",
    "                      ['KNN','Decision Tree','LogisticRegression','VotingClassifier']):  \n",
    "  \n",
    "    scores = model_selection.cross_val_score(clf, x_data, y_data, cv=3, scoring='accuracy')  \n",
    "    print(\"Accuracy: %0.2f [%s]\" % (scores.mean(), label)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
